package com.acme.bankapp.domain.bank;

import com.acme.bankapp.event.listener.ClientRegistrationListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Class representing bank
 */
public class Bank {

    private List<Client> clients;

    private List<ClientRegistrationListener> clientRegistrationListeners;

    /**
     * Instantiate bank with no clients
     */
    public Bank(){
        this.clients = new ArrayList<Client>();
        this.clientRegistrationListeners = new ArrayList<ClientRegistrationListener>();
    }

    /**
     * Create bank with preset clients
     * @param clients Initial clients
     */
    public Bank(List<Client> clients){
        this.clients = clients;
    }

    /**
     * Adds listeners on addClient event
     * @param clientRegistrationListeners listener to add
     */
    public void addClientRegistrationListener(ClientRegistrationListener clientRegistrationListeners) {
        this.clientRegistrationListeners.add(clientRegistrationListeners);
    }

    /**
     * @param client Client to add
     * @return Just added client
     */
    public boolean addClient(Client client){
        for(ClientRegistrationListener listener:clientRegistrationListeners){
            listener.onClientAdded(client);
        }
        return clients.add(client);
    }

    /**
     * Removes client from Bank
     * @param client Client remove
     * @return removed client
     */
    public boolean removeClient(Client client){
        return  clients.remove(client);
    }

    /**
     * Returns all clients
     * @return All bank clients
     */
    public Collection<Client> getAllClients(){
        return clients;
    }

    /**
     * Inner class - printer listener
     */
    public class PrintClientListener implements ClientRegistrationListener{
        /**
         * Self - registering constructor.
         * Istantiates listener and add it to bank listeners
         */
        public PrintClientListener() {
            clientRegistrationListeners.add(this);
        }

        @Override
        public void onClientAdded(Client client) {
            System.out.println(String.format("New client named %s was added",client.getName()));
        }
    }

    /**
     * Inner class - email notification listener
     */
    public class EmailNotificationListener implements ClientRegistrationListener{
        /**
         * Self - registering constructor
         */
        public EmailNotificationListener() {
            clientRegistrationListeners.add(this);
        }

        @Override
        public void onClientAdded(Client client) {
            System.out.println(String.format("Email was sent to %s",client.getName()));
        }
    }
}
